# vim:ts=4:sw=4:noet:
""" New Method dialog """

import wx
from c.ui.wxw.widgets.checkedentry import CheckedEntry
from c.base.support import CoreException

class NewMethodDialog(wx.Dialog):

	def __init__(self, parent, classes, chosenClass=None, methSig="", isClassMeth=False):
		wx.Dialog.__init__(self, parent, -1, "New Method")
		self.mw = parent
		self.methcls = wx.Choice(self, -1, choices=classes)
		if chosenClass:
			self.methcls.SetSelection(classes.index(chosenClass))
		else:
			self.methcls.SetSelection(0)

		self.clsmethCheck = wx.CheckBox(self, -1, "Class Method")
		self.clsmethCheck.SetValue(isClassMeth)

		self.entry = CheckedEntry(self, self.doCheck, self.doDone)
		
		self.ok = wx.Button(self, wx.ID_OK, "OK")
		self.ok.SetDefault()
		self.ok.Enable(False)
		cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
		
		dlgsizer = wx.BoxSizer(wx.VERTICAL)
		dlgsizer.Add(self.methcls, 0, wx.ALL | wx.EXPAND, 10)
		dlgsizer.Add(self.clsmethCheck, 0, wx.LEFT | wx.RIGHT | wx.BOTTOM, 10)
		dlgsizer.Add(self.entry, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)

		btnsizer = wx.StdDialogButtonSizer()
		btnsizer.AddButton(self.ok)
		btnsizer.AddButton(cancel)
		btnsizer.Realize()
		
		dlgsizer.Add(btnsizer, 0, wx.ALL | wx.ALIGN_RIGHT, 10)
		
		self.SetSizer(dlgsizer)

		self.Fit()
		self.Centre()

	def GetValue(self):
		txt = self.entry.GetValue()
		cls = self.methcls.GetString(self.methcls.GetSelection())
		return (cls, self.clsmethCheck.IsChecked(), txt)

	def doCheck(self, sig):
		try:
			clsname = self.methcls.GetString(self.methcls.GetSelection())
			cls = self.mw.controller.mem.getGlobal(clsname)
			methname = self.mw.controller.compiler.checkMethodSig(sig)
			if cls.hasMethod(methname):
				self.ok.Enable(False)
				return False, "Method '%s' already exists" % methname
			self.ok.Enable(True)
			return True, ""
		except CoreException,e:
			self.ok.Enable(False)
			return False, str(e)
	
	def doDone(self):
		if self.ok.IsEnabled():
			self.EndModal(wx.ID_OK)

